/**
 * 
 */
package operators;

import implementation.Node;

/**
* <br><b>Operator</b>Executes operations on the parse tree 
* </br>
* <b>Extends</b> Node
* @author Tarca Alexandra
* @version 1.6
* @since 30/4/2011
*/
public class Operator extends Node {
	/**
	 * @param val - the string representation of the operator
	 */
	public Operator(String val) {
		super(val);
	}


	/**
	 * @return the double value of the parse tree computation
	 * @param void
	 */
	public Double compute(){
		if (getContent().equals("+"))
			return Double.parseDouble(getLeft().getContent())+ Double.parseDouble(getRight().getContent());
		if (getContent().equals("-"))
			return Double.parseDouble(getLeft().getContent())- Double.parseDouble(getRight().getContent());
		if (getContent().equals("/"))
			return Double.parseDouble(getLeft().getContent())/ Double.parseDouble(getRight().getContent());
		if (getContent().equals("*"))
			return Double.parseDouble(getLeft().getContent())* Double.parseDouble(getRight().getContent());
		if (getContent().equals("$"))                                                                                                                    				 //the money sign
			return Double.parseDouble(getLeft().getContent())+ Double.parseDouble(getRight().getContent())-2*Double.parseDouble(getLeft().getContent())*Double.parseDouble(getRight().getContent())-2;
		return 0.0;
	}

	/**
	 * @return the double value of the parse tree computation derived
	 * @param void
	 */
	@Override
	public Double computeDerivate() {
		if (getContent().equals("+"))
			return getLeft().computeDerivate()+ getRight().computeDerivate();
		if (getContent().equals("-"))
			return getLeft().computeDerivate()- getRight().computeDerivate();
		if (getContent().equals("/"))
			return getLeft().computeDerivate()/ getRight().computeDerivate();
		if (getContent().equals("*"))
			return getLeft().computeDerivate()* getRight().computeDerivate();
		return 0.0;
	}


	/**
	 * <b>Recursive</b>
	 * @return the String value of the derived parse tree  
	 * @param void
	 */
	@Override
	public String evalDerivate() {
		return super.getLeft().evalDerivate() + super.getContent()+ super.getRight().evalDerivate();
	}
}
